-
Notifications
You must be signed in to change notification settings - Fork 92
gppa-preserve-selections.php: Fixed an issue with preserve selections not working correctly for checkbox field.
#1114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gppa-preserve-selections.php: Fixed an issue with preserve selections not working correctly for checkbox field.
#1114
Conversation
…ns not working correctly for checkbox field.:
WalkthroughThe code was updated to handle checkbox fields with sub-keys when retrieving selected values from requests. It now scans for keys matching the field ID pattern for checkboxes. Additionally, a filter was added to disable the use of posted values for field choices for specific form and field IDs. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant FormHandler
participant FieldLogic
User->>FormHandler: Submit form with checkbox fields
FormHandler->>FieldLogic: Retrieve selected values from request
alt Field is checkbox
FieldLogic->>FieldLogic: Scan request for keys matching "fieldID.*"
FieldLogic->>FormHandler: Return array of selected sub-keys
else Other field types
FieldLogic->>FormHandler: Return selected value(s) directly
end
FormHandler->>FieldLogic: Apply filter to disable posted values for specific field
FieldLogic->>FormHandler: Return choices without using posted values (if filter is active)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
gp-populate-anything/gppa-preserve-selections.php (1)
30-30: Ensure filter consistencyThe new filter uses the same form ID (123) and field ID (4) as the main filter above. Verify these IDs are updated consistently when customizing the snippet.
Consider adding a comment to clarify the purpose of this filter:
// Update "123" to your form ID and "4" to your field ID. +// Disable posted values to prevent conflicts with custom selection logic above add_filter( 'gppa_field_choices_posted_value_123_4', '__return_false' );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-populate-anything/gppa-preserve-selections.php(1 hunks)
🔇 Additional comments (2)
gp-populate-anything/gppa-preserve-selections.php (2)
8-9: LGTM: Clean field value retrievalThe field value retrieval logic is straightforward and follows standard practices.
21-21:❌ Incorrect review comment
Verify array structure compatibility
The
in_array()check assumes$selected_valuesis a simple array of values, but the checkbox logic above creates an associative array. This could cause the selection check to fail for checkbox fields.
🏁 Script executed:
#!/bin/bash # Search for similar checkbox handling patterns in the codebase rg -A 10 -B 5 "checkbox.*selected_values|selected_values.*checkbox" --type phpLength of output: 1546
in_array() works with associative arrays—no change needed
PHP’sin_array()checks array values, not keys, so using it against an associative$selected_valueswill still correctly detect matching checkbox values.Likely an incorrect or invalid review comment.
| if ( $field->type == 'checkbox' && ! empty( $selected_values ) ) { | ||
| // look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id | ||
| $selected_values = array(); | ||
| foreach ( $field_values as $key => $value ) { | ||
| if ( strpos( (string) $key, $field->id . '.' ) === 0 ) { | ||
| $selected_values[ $key ] = $value; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic issue: Inconsistent array structure for checkbox processing
The checkbox handling logic changes the structure of $selected_values from a simple array to an associative array with keys like "5.1" and their corresponding values. This creates a mismatch with the in_array() check on line 21.
if ( $field->type == 'checkbox' && ! empty( $selected_values ) ) {
- // look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
- $selected_values = array();
- foreach ( $field_values as $key => $value ) {
- if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
- $selected_values[ $key ] = $value;
- }
- }
+ // look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
+ $checkbox_values = array();
+ foreach ( $field_values as $key => $value ) {
+ if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
+ $checkbox_values[] = $value;
+ }
+ }
+ $selected_values = $checkbox_values;
}This ensures $selected_values remains a simple array of values, maintaining compatibility with the in_array() check below.
🤖 Prompt for AI Agents
In gp-populate-anything/gppa-preserve-selections.php around lines 10 to 18, the
checkbox handling code changes $selected_values from a simple array to an
associative array with keys like "5.1", causing incompatibility with the
in_array() check later. To fix this, modify the loop to append values to
$selected_values as a simple indexed array instead of using keys, preserving the
original array structure and ensuring in_array() works correctly.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2954151326/84317
Summary
The snippet to preserve the selected choice on repopulation, doesn't work on Checkbox fields.
This update needs GPPA PR: https://github.com/gravitywiz/gp-populate-anything/pull/625
https://www.loom.com/share/4128bac6460b4892b0a0b710ded0069c